<?
/****************************************/
/************* IdeaLAN LLC **************/
/* Services where you can manage your   */
/* invoices and services.               */
/*                                      */
/****************************************/
/****************************************/
/* SCRIPT: services.php                 */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
if (file_exists("services.php")) {	$mod_lud = filemtime("services.php"); }		
$mod_ver	=	"2.5.1";
/****************************************/
/*
TODO +
	 - MAKE QUICK SEARCH TO BE USED ONCE.
	 - ADD SORTING BY INV# (default) OPEN DATE MAYBE NAME
	 - MAKE SOMETHING SO IT TELLS YOU THAT THE EMAIL HAS BEEN SENT WITH A DATE OF EMAILING.
	 - AND POSSIBLY MAKE SOMETHING WHEN YOU FINISH WITH THE INVOICE LOCK IT. BUT WHEN YOU NEED TO MAKE A CHANGE MARK IT AS A REVISION.
END TODO
*/
//$path_base = exec("pwd");

/* ----------- INSTALLATION ----------- */

if ($_GET[view] == "install") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Services' AND `mod_location` = 'services.php' AND `mod_ver` = '$mod_ver'";  
	$exexute_check_module = mysql_query($check_module); 
	
	if ($exexute_check_module != "") {
		if (mysql_num_rows($exexute_check_module) == 0) { 
			
			
			/* *** CREATE SERVICES TABLE *** */
			$table_services = "CREATE TABLE `".$DBprefix."services` (
			`serv_id` SMALLINT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`serv_group` SMALLINT(5) NOT NULL ,
			`serv_name` VARCHAR(50) NOT NULL ,
			`serv_discount` VARCHAR(50) NOT NULL ,
			`serv_bprice` VARCHAR(50) NOT NULL ,
			`serv_desc` VARCHAR(250) NOT NULL ,
			`serv_addby` VARCHAR(50) NOT NULL)";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_services);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>SERVICES</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}



			/* *** CREATE GROUPS TABLE *** */
			$table_service_groups = "CREATE TABLE `".$DBprefix."service_groups` (
			`group_id` SMALLINT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`group_name` VARCHAR(50) NOT NULL ,
			`group_desc` VARCHAR(250) NOT NULL ,
			`group_addby` VARCHAR(50) NOT NULL)";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_service_groups);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>SERVICE GROUPS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}

			/* *** CREATE SERVICE ORDERS DETAILS TABLE *** */
			$table_service_orders = "CREATE TABLE IF NOT EXISTS `".$DBprefix."service_details` (
			`sod_id` smallint(5) NOT NULL auto_increment,
			`sod_soid` smallint(5) NOT NULL,
			`sod_servid` smallint(5) NOT NULL,
			`sod_servname` VARCHAR(50) NOT NULL,
			`sod_servprice` VARCHAR(50) NOT NULL,
			`sod_discount` VARCHAR(50) NOT NULL,
			`sod_shipping` VARCHAR(50) NOT NULL,
			`sod_tax` VARCHAR(50) NOT NULL,
			`sod_qty` VARCHAR(50) NOT NULL,
			`sod_total` VARCHAR(50) NOT NULL,
			`sod_addby` smallint(5) NOT NULL,
			PRIMARY KEY (`sod_id`),
			KEY `sod_soid` (`sod_soid`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";
			
			

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_service_orders);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>SERVICE ORDER DETAILS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
 
 			/* *** CREATE SERVICE ORDERS TABLE *** */
			$table_service_orders = "CREATE TABLE IF NOT EXISTS `".$DBprefix."service_orders` (
			`so_id` smallint( 5 ) NOT NULL AUTO_INCREMENT ,
			`so_number` VARCHAR( 20 ) NOT NULL ,
			`so_type` VARCHAR( 10 ) NOT NULL ,
			`so_cid` smallint( 5 ) NOT NULL ,
			`so_did` smallint( 5 ) NOT NULL ,
			`so_pname` VARCHAR( 100 ) NOT NULL ,
			`so_discount` VARCHAR( 50 ) NOT NULL ,
			`so_tax` SMALLINT( 2 ) NOT NULL default 0,
			`so_note` text NOT NULL ,
			`so_tnote` text NOT NULL ,
			`so_paytype` tinyint( 4 ) NOT NULL default '10',
			`so_status` tinyint( 4 ) NOT NULL ,
		  	`so_status_reason` VARCHAR(255) NOT NULL default 'Not Paid!',
			`so_opendate` VARCHAR( 50 ) NOT NULL ,
			`so_paydate` VARCHAR( 50 ) NOT NULL ,
			`so_closedate` VARCHAR( 50 ) NOT NULL ,
			`so_refunddate` VARCHAR( 50 ) NOT NULL ,
			`so_addby` smallint( 5 ) NOT NULL ,
			PRIMARY KEY ( `so_id` ) ,
			KEY `so_number` ( `so_number` )
			) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1";
						

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_service_orders);

			// LETS ADD FIRST INVOICE NUMBER
			$add_firstInv = "INSERT INTO `".$DBprefix."service_orders` (`so_id`,`so_number`,`so_cid`,`so_did`,`so_discount`,`so_note`,`so_status`,`so_status_reason`,`so_opendate`,`so_paydate`,`so_closedate`,`so_refunddate`,`so_addby`) VALUES ('1','0','0','0','0','0','0','0','0','0','0','0','1')";  
			$execute_add_firstInv = mysql_query($add_firstInv);

	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>SERVICE ORDERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
 
 
 
			/* ---------- ADD MODULE TO MODULES LIST---------- */			
			// LETS CHECK IF THERE ARE ANY MODULES INSTALLED IF NOT LETS MAKE THIS ONE #1
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` DESC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			if ($row_count != 0) {
			if ($module = mysql_fetch_array($execute_get_modules)){

				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
			
			}
			
			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('$row_count' + 1,'Services','services.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			} else {

			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('1','Services','services.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			}

				
			header("Location: ../../in.php?show=config&view=modules&m=20");
			die();
				
		} else {
			echo "ADDING MODULE: <i><b>SERVICES</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already added.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END INSTALL





/* -------- END INSTALLATION --------- */

/* ----------- REMOVAL ----------- */

if ($_GET[view] == "remove") {

        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");


	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Services' AND `mod_location` = 'services.php'";  
	$exexute_check_module = mysql_query($check_module); 
	
	if ($exexute_check_module != "") {
		if (mysql_num_rows($exexute_check_module) > 0) { 
			
			
			/* *** DROP SERVICES TABLE *** */
			$table_services = "DROP TABLE `".$DBprefix."services`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_services);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>SERVICES</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}

			/* *** DROP GROUPS TABLE *** */
			$table_groups = "DROP TABLE `".$DBprefix."service_groups`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_groups);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>GROUPS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
			
			
			/* *** DROP GROUPS TABLE *** */
			$table_orders = "DROP TABLE `".$DBprefix."service_orders`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_orders);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>SERVICE ORDERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}			
			
 
 			/* *** DROP GROUPS TABLE *** */
			$table_details = "DROP TABLE `".$DBprefix."service_details`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_details);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>ORDER DETAILS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
 

			/* ---------- REMOVE MODULE FROM MODULES ---------- */	
			$remove_module = "DELETE FROM `".$DBprefix."modules` WHERE `mod_name` = 'Services' AND `mod_location` = 'services.php'";
			$remove_result = mysql_query($remove_module);
			
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` ASC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			while ($module = mysql_fetch_array($execute_get_modules)){
				$i = $i + 1;
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
			
			$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_id` = '$i'  WHERE `mod_location` = '$mod_location'";
			$execute_upodate_modules = mysql_query($update_modules);
			
			}
				
			header("Location: ../../in.php?show=config&view=modules&m=21");
			die();
				

		} else {
			echo "REMOVING MODULE: <i><b>SERVICES</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already gone.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back.</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END REMOVAL

/* -------- BACK UP THE MODULE ------------ */
if ($_GET[view] == "backup") {

        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");

	$backup_file = "Services_". date("d-m-Y_H-i-s")  . ".gz";
	$backup = "mysqldump --opt -h $server -u$username -p$password $database $DBprefix"."services $DBprefix"."service_groups $DBprefix"."service_orders $DBprefix"."service_details | gzip > $backup_file";
	system($backup);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=22");
	die();	
	
} // END BACKUP


/* ----------- RESTORE MODULE ---------- */
if ($_GET["view"] == "restore") {
	
        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Services' AND `mod_location` = 'services.php'";  
	$execute_check_module = mysql_query($check_module); 
	
	
	if (mysql_num_rows($execute_check_module) != "0") {
					

		$restore_file = $_GET["file"];
		$restore = "zcat $restore_file | mysql -u$username -p$password $database";
		system($restore);

	
		header("Location: ../../in.php?show=config&view=modules&m=25");
		die();	
		


	} else {
		header("Location: ../../in.php?show=config&view=modules&e=13");
		die();	
	 }

	
} // END RESTORE

/* --------- CHANGE ORDER ------------ */
if ($_GET["view"] == "order") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	if ($_GET["change"] == "down") {
	
		$new_order = $_GET["modid"] + 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'Services' AND `mod_location` =  'services.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=31");
		die();
	}
	
	if ($_GET["change"] == "up") {

		$new_order = $_GET["modid"] - 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'Services' AND `mod_location` =  'services.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=32");
		die();
	
	}


} // END CHANGE ORDER

/* --------- UPDATE VERSION ------------ */

if ($_GET["view"] == "verup") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	

	$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_ver` = '$mod_ver',`mod_lud` = '$mod_lud' WHERE `mod_location` = 'services.php'";
	$execute_upodate_modules = mysql_query($update_modules);
	
			
	header("Location: ../../in.php?show=config&view=modules&m=33");
	die();
			
} // END UPDATE VERSION


/* -------- END INSTALATION --------- */


if (empty($_SESSION["sesid"])) { header ("Location: ../../in.php?e=0"); }

?>

		<div id="content">
					<div id="left">
							<div class="naglowek"><div class="pkt"></div>Services</div>
							<ul id="me">
									<li <? if ($view == "") { echo "id=\"press\""; } ?> ><a href="in.php?show=mod&modid=<? echo $modid; ?>">Open Service Orders</a></li>
									<li <? if ($view == "closed") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=closed&modid=<? echo $modid; ?>">Closed Service Orders</a></li>
									<li <? if ($view == "add") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=add&modid=<? echo $modid; ?>">New Service</a></li>
									<li <? if ($view == "quotes") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=quotes&modid=<? echo $modid; ?>">Quotes</a></li>
									<li <? if ($view == "services") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=services&modid=<? echo $modid; ?>">Services</a></li>
									<li <? if ($view == "groups") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=groups&modid=<? echo $modid; ?>">Groups</a></li>
							</ul>
					
					
					</div>					
					
					<div id="right">
<?
if (!$view) {

// ---- START QUICK SEARCH ---- //
$sq = $_POST["sq"];
$sq_soStatus = "0"; // STATUS OF THE SO 0=OPEN

if ($sq != "" AND is_numeric($sq)) { 
	// SEARCH FOR PHONE # OR INV #
	//echo "is a number";
	
	// SEARCH FOR A PHONE #
	if (strlen($sq) > "6" AND strlen($sq) <= "11") { 
	
		$sq_cust = "SELECT `cust_id` FROM `".$DBprefix."customers` WHERE `cust_1phone` LIKE '%$sq%' OR `cust_2phone` LIKE '%$sq%'";  
		$result = mysql_query($sq_cust);
		$row = mysql_fetch_array($result);
		$sq_custid = $row["cust_id"];
		
		$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` LIKE  '$sq_custid' AND `so_status` = '$sq_soStatus'";
	
	}
	
	// SEARCH FOR A INV #
	if (strlen($sq) <= "6") { $search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_number` LIKE  '%$sq%' AND `so_status` = '$sq_soStatus'"; } 

} else if ($sq != "" AND $sq != " ") {
	// SEARCH THROUGH CUTOMER INFO NAME.
	//echo "is a character";
	
	if (strstr($sq, " ")) { $sq_fn = 1; } else { $sq_fn = 0; }
	list ($sqfname, $sqlname) = explode(" ", $sq);
	
	if ($sq_fn == 1 AND !empty($sqfname) AND !empty($sqlname)) {
		// SEARCH FOR FIRST AND LAST NAME
		//echo " look for fname AND lname";
		
		list ($sqfname, $sqlname) = explode(" ", $sq);
		
		$sq_cust = "SELECT `cust_id` FROM `".$DBprefix."customers` WHERE `cust_fname` LIKE '%$sqfname%' AND `cust_lname` LIKE '%$sqlname%'";  
		$result = mysql_query($sq_cust);
		$row = mysql_fetch_array($result);
		$sq_custid = $row["cust_id"];
		
		$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` LIKE  '$sq_custid' AND `so_status` = '$sq_soStatus'";
	} else {
	// SEARCH FOR FIRST OR LAST NAME
		//echo " look for fname OR lname";
		
		$sq = str_replace(" ", "", $sq);
		
		$sq_cust = "SELECT `cust_id` FROM `".$DBprefix."customers` WHERE `cust_fname` LIKE '%$sq%' OR `cust_lname` LIKE '%$sq%'";  
		$result = mysql_query($sq_cust);
		$row = mysql_fetch_array($result);
		$sq_custid = $row["cust_id"];
		
		$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` LIKE  '$sq_custid' AND `so_status` = '$sq_soStatus'";
	}
	
} else {
	// DISPLAY ALL ENTRIES
	$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_status` = '$sq_soStatus'";
}

// ---- END QUICK SEARCH ---- //

// LETS GET SERVICE ORDER DATA
$get_so = "$search_query";  
$execute_get_so = mysql_query($get_so); 
$get_soResult = mysql_num_rows($execute_get_so);

	if (empty($get_soResult)) { $sq_msg = "<br/>( !!! NOTHING FOUND !!! )"; } else { $sq_msg = "<br />(quick search looks for phone #'s first, last name and INV #)"; }

echo "
							<div class='naglowek'><div class='pkt'></div>OPEN SERVICE ORDERS</div>
							<div id='rightcont'>
<form method='POST' name='sq_form' action='in.php?show=mod&modid=$modid'>

<center><input name=\"sq\" id=\"sq\" type=\"text\" class=\"input_QuickSearch\" autocomplete=\"off\"> <input type=\"submit\" value=\"QUICK SEARCH\" class=\"button_QuickSearch\">$sq_msg</center><br/>

</form>
								<table class='editform1' style='width: 100%'>
       	 <tr>
	  		<td width='50' class='tableLogsHeader'>INV #</td>
			<td width='200' class='tableLogsHeader'>CUSTOMER</td>
  			<td width='120' class='tableLogsHeader'>PHONE</td>
  			<td width='80' class='tableLogsHeader'>STATUS</td>
  			<td width='125' class='tableLogsHeader'>OPEN DATE</td>
			<td width='125' class='tableLogsHeader'>DUE DATE</td>
			<td width='100' class='tableLogsHeader'>TOTAL DUE</td>
			<td width='150' class='tableLogsHeader'>OPTIONS</td>
		</tr>



";


// LETS PROCESS THE INFO
while ($so = mysql_fetch_array($execute_get_so)){
	$so_id = $so["so_id"];
	$so_number = $so["so_number"];
	$so_cid = $so["so_cid"];
	$so_did = $so["so_did"];
	$so_discount = $so["so_discount"];
	$so_tax = $so["so_tax"];
	$so_status = $so["so_status"];
	$so_status_reason = $so["so_status_reason"];
	$so_opendate = date("m.d.y", $so["so_opendate"]);
	$so_paydate = date("m.d.y", $so["so_paydate"]);
	$so_closedate = $so["so_closedate"];
	$so_refunddate = $so["so_refunddate"];
	$so_addby = $so["so_addby"];



	// LETS GET CUSTOMER DATA FROM DATABASE
	$get_customer = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_id` = '$so_cid' LIMIT 1";  
	$execute_get_customer = mysql_query($get_customer); 

	if ($customer = mysql_fetch_array($execute_get_customer)){
	$cust_id = $customer["cust_id"];
	$cust_fname = $customer["cust_fname"];
	$cust_lname = $customer["cust_lname"];
	$cust_1phone = format_phone($customer["cust_1phone"]);
	$cust_city = $customer["cust_city"];
	$cust_state = $customer["cust_state"];
	
	if ($so_status <= 0) { $so_status = "UNPAID"; $mark = "red"; } else if ($so_status == 1) { $so_status = "PAID"; } else if ($so_status == 2) { $so_status = "REFUND"; } else if ($so_status == 3) { $so_status = "CANCELED"; } else { $so_status = "UNPAID"; }

///////////////////////////////////////////////

// WE NEED TO RESET THE VARIABLES TO 0

	$so_total = "";
	$item_subtotal = "";
	$item_discount = "";
	$item_total = "";
	$so_shTotal = "";
	$so_subTotal = "";
	$so_discTotal = "";
	$so_taxTotal = "";

// LETS GET SO DETAILS
$get_sod = "SELECT * FROM `".$DBprefix."service_details` WHERE `sod_soid` = '$so_id'";  
$execute_get_sod = mysql_query($get_sod); 

while ($sod = mysql_fetch_array($execute_get_sod)){
	
	
	$sod_servprice = $sod["sod_servprice"];
	$sod_discount = $sod["sod_discount"];
	$sod_shipping = $sod["sod_shipping"];
	$sod_tax = $sod["sod_tax"];
	$sod_qty = $sod["sod_qty"];
	$sod_total = $sod["sod_total"];
	

	$item_subtotal = $sod_servprice * $sod_qty;
	$item_discount = ($sod_discount / 100) * $item_subtotal;
	
	$item_total = $item_subtotal - $item_discount;
	
	$so_shTotal = $so_shTotal + $sod_shipping;
	$so_subTotal = $so_subTotal + $item_total;

$so_discTotal = ($so_discount / 100) * $so_subTotal;
if ($so_tax == 1) { $so_taxTotal = $so_subTotal * 0.06;}

$so_total = $so_subTotal + $so_shTotal + $so_taxTotal - $so_discTotal;

}

// LETS GET MODULES THAT ARE IN THE SYSTEM
	$get_modules = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` LIKE 'Customers'";  
	$execute_get_modules = mysql_query($get_modules); 
	
	while ($module = mysql_fetch_array($execute_get_modules)){
		$mod_id = $module["mod_id"];
		$mod_name = $module["mod_name"];
		$mod_location = $module["mod_location"];
		
		if (file_exists("inc/modules/$mod_location")) { 

		$short_customerdetail = "<a href='?show=mod&view=detail&modid=$mod_id&cid=$cust_id'>$cust_lname $cust_fname</a>";	


		}

	}

///////////////////////////////////////////////
if ($so_total == 0) { $so_total = "0"; }

	echo "

       	 <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
	  		<td class='tableLogs'>$so_number</td>
	  		<td class='tableLogs'>$short_customerdetail</td>
  			<td class='tableLogs'>$cust_1phone</td>
  			<td class='tableLogs$mark'>&nbsp;$so_status</td>
  			<td class='tableLogs'>$so_opendate</td>
  			<td class='tableLogs'>$so_paydate</td>
			<td class='tableLogs'>$" . number_format($so_total, 2, '.', ','). "</td>
			<td class='tableLogs'>$extra_links <a href='?show=mod&view=detail&soid=$so_id&modid=$modid' onmouseover=\"Tip('Edit Inv.')\"><img src='../g/ico/so.png'> </a> <A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A><a href='print.php?soid=$so_id&to=I' target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </a> <a href=\"javascript:confirmDelete('modify.php?act=change&what=services&cat=so&soid=$so_id&status=1&modid=$modid','MARK AS PAID: $so_number')\" onmouseover=\"Tip('Mark As PAID')\"><img src='../g/ico/money_add.png'></a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id&modid=$modid','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Remove Inv.')\"><img src='../g/ico/so_delete.png'></a></td>
		</tr>

	";

} // END GET CUSTOMER DATA
	

} // END GET SO DATA
echo "</table>";

} // END OPEN S/O



if ($view == "closed") {

// ---- START QUICK SEARCH ---- //
$sq = $_POST["sq"];
$sq_soStatus = "1"; // STATUS OF THE SO 0=OPEN

if ($sq != "" AND is_numeric($sq)) { 
	// SEARCH FOR PHONE # OR INV #
	//echo "is a number";
	
	// SEARCH FOR A PHONE #
	if (strlen($sq) > "6" AND strlen($sq) <= "11") { 
	
		$sq_cust = "SELECT `cust_id` FROM `".$DBprefix."customers` WHERE `cust_1phone` LIKE '%$sq%' OR `cust_2phone` LIKE '%$sq%'";  
		$result = mysql_query($sq_cust);
		$row = mysql_fetch_array($result);
		$sq_custid = $row["cust_id"];
		
		$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` LIKE  '$sq_custid' AND `so_status` >= '$sq_soStatus'";
	
	}
	
	// SEARCH FOR A INV #
	if (strlen($sq) <= "6") { $search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_number` LIKE  '%$sq%' AND `so_status` >= '$sq_soStatus'"; } 

} else if ($sq != "" AND $sq != " ") {
	// SEARCH THROUGH CUTOMER INFO NAME.
	//echo "is a character";
	
	if (strstr($sq, " ")) { $sq_fn = 1; } else { $sq_fn = 0; }
	list ($sqfname, $sqlname) = explode(" ", $sq);
	
	if ($sq_fn == 1 AND !empty($sqfname) AND !empty($sqlname)) {
		// SEARCH FOR FIRST AND LAST NAME
		//echo " look for fname AND lname";
		
		list ($sqfname, $sqlname) = explode(" ", $sq);
		
		$sq_cust = "SELECT `cust_id` FROM `".$DBprefix."customers` WHERE `cust_fname` LIKE '%$sqfname%' AND `cust_lname` LIKE '%$sqlname%'";  
		$result = mysql_query($sq_cust);
		$row = mysql_fetch_array($result);
		$sq_custid = $row["cust_id"];
		
		$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` LIKE  '$sq_custid' AND `so_status` >= '$sq_soStatus'";
	} else {
	// SEARCH FOR FIRST OR LAST NAME
		//echo " look for fname OR lname";
		
		$sq = str_replace(" ", "", $sq);
		
		$sq_cust = "SELECT `cust_id` FROM `".$DBprefix."customers` WHERE `cust_fname` LIKE '%$sq%' OR `cust_lname` LIKE '%$sq%'";  
		$result = mysql_query($sq_cust);
		$row = mysql_fetch_array($result);
		$sq_custid = $row["cust_id"];
		
		$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` LIKE  '$sq_custid' AND `so_status` >= '$sq_soStatus'";
	}
	
} else {
	// DISPLAY ALL ENTRIES
	$search_query = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_status` >= '$sq_soStatus'";
}

// ---- END QUICK SEARCH ---- //

// LETS GET SERVICE ORDER DATA
$get_so = "$search_query";  
$execute_get_so = mysql_query($get_so); 
$get_soResult = mysql_num_rows($execute_get_so);

	if (empty($get_soResult) AND $sq != "") { $sq_msg = "<br/>( !!! NOTHING FOUND !!! )"; } else { $sq_msg = "<br />(quick search looks for phone #'s first, last name and INV #)"; }
echo "
							<div class='naglowek'><div class='pkt'></div>CLOSED SERVICE ORDERS</div>
							<div id='rightcont'>
<form method='POST' name='sq_form' action='in.php?show=mod&modid=$modid&view=closed'>

<center><input name=\"sq\" type=\"text\" class=\"input_QuickSearch\" autocomplete=\"off\"> <input type=\"submit\" value=\"QUICK SEARCH\" class=\"button_QuickSearch\">$sq_msg</center><br/>

</form>
								<table class='editform1' style='width: 100%'>
       	 <tr>
	  		<td width='50' class='tableLogsHeader'>INV #</td>
			<td width='200' class='tableLogsHeader'>CUSTOMER</td>
  			<td width='150' class='tableLogsHeader'>PHONE</td>
  			<td width='100' class='tableLogsHeader'>STATUS</td>
  			<td width='125' class='tableLogsHeader'>OPEN DATE</td>
			<td width='125' class='tableLogsHeader'>PAID DATE</td>
			<td width='100' class='tableLogsHeader'>TOTAL PAID</td>
			<td width='100' class='tableLogsHeader'>OPTIONS</td>
		</tr>



";


while ($so = mysql_fetch_array($execute_get_so)){
	$so_id = $so["so_id"];
	$so_number = $so["so_number"];
	$so_cid = $so["so_cid"];
	$so_did = $so["so_did"];
	$so_discount = $so["so_discount"];
	$so_tax = $so["so_tax"];
	$so_status = $so["so_status"];
	$so_status_reason = $so["so_status_reason"];
	$so_opendate = date("m.d.y", $so["so_opendate"]);
	$so_paydate = date("m.d.y", $so["so_paydate"]);
	$so_closedate = $so["so_closedate"];
	$so_refunddate = $so["so_refunddate"];
	$so_addby = $so["so_addby"];

///////////////////////////////////////////////

// WE NEED TO RESET THE VARIABLES TO 0

	$so_total = "";
	$item_subtotal = "";
	$item_discount = "";
	$item_total = "";
	$so_shTotal = "";
	$so_subTotal = "";
	$so_discTotal = "";
	$so_taxTotal = "";

// LETS GET SO DETAILS
$get_sod = "SELECT * FROM `".$DBprefix."service_details` WHERE `sod_soid` = '$so_id'";  
$execute_get_sod = mysql_query($get_sod); 

while ($sod = mysql_fetch_array($execute_get_sod)){
	
	
	$sod_servprice = $sod["sod_servprice"];
	$sod_discount = $sod["sod_discount"];
	$sod_shipping = $sod["sod_shipping"];
	$sod_tax = $sod["sod_tax"];
	$sod_taxtotal = $sod["sod_taxtotal"];
	$sod_qty = $sod["sod_qty"];
	$sod_total = $sod["sod_total"];
	

	$item_subtotal = $sod_servprice * $sod_qty;
	$item_discount = ($sod_discount / 100) * $item_subtotal;
	
	$item_total = $item_subtotal - $item_discount;
	
	$so_shTotal = $so_shTotal + $sod_shipping;
	$so_subTotal = $so_subTotal + $item_total;

$so_discTotal = ($so_discount / 100) * $so_subTotal;

if ($so_tax == 1) { $so_taxTotal = $so_subTotal * 0.06;}

$so_total = $so_subTotal + $so_shTotal + $so_taxTotal - $so_discTotal;

}

///////////////////////////////////////////////


	// LETS GET CUSTOMER DATA FROM DATABASE
	$get_customer = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_id` = '$so_cid' LIMIT 1";  
	$execute_get_customer = mysql_query($get_customer); 

	if ($customer = mysql_fetch_array($execute_get_customer)){
	$cust_id = $customer["cust_id"];
	$cust_fname = $customer["cust_fname"];
	$cust_lname = $customer["cust_lname"];
	$cust_1phone = format_phone($customer["cust_1phone"]);
	$cust_city = $customer["cust_city"];
	$cust_state = $customer["cust_state"];

	
	if ($so_status <= 0) { $so_status = "UNPAID"; } else if ($so_status == 1) { $so_status = "PAID"; $mark = "green";} else if ($so_status == 2) { $so_status = "REFUND"; $mark = "yellow"; $so_total = "-$so_total"; } else if ($so_status == 3) { $so_status = "CANCELED"; $so_total = "0"; $mark = "red";} else { $so_status = "UNPAID"; }

	$total_made = $total_made + $so_total;

	if ($so_total == 0) { $so_total = "0"; }
	
	echo "

       	 <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
	  		<td class='tableLogs'>$so_number</td>
	  		<td class='tableLogs'>$cust_lname $cust_fname</td>
  			<td class='tableLogs'>$cust_1phone</td>
  			<td class='tableLogs$mark'>$so_status</td>
  			<td class='tableLogs'>$so_opendate</td>
  			<td class='tableLogs'>$so_paydate</td>
			<td class='tableLogs'>$" .number_format($so_total, 2, '.', ','). " </td>
			<td class='tableLogs'>$extra_links <A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A><a href='?show=mod&view=detail&soid=$so_id&modid=$modid' onmouseover=\"Tip('Edit Inv.')\"><img src='../g/ico/so.png'> </a>  </td>
		</tr>

	";

} // END GET CUSTOMER DATA
	

} // END GET SO DATA

echo " $" .number_format($total_made, 2, '.', ','). "";



// LETS GET CUSTOMER DEVICE INFO
$get_device = "SELECT * FROM `".$DBprefix."devices` WHERE `dev_id` = '$so_did' LIMIT 1";  
$execute_get_device = mysql_query($get_device); 

if ($device = mysql_fetch_array($execute_get_device)){
$dev_id = $device["dev_id"];
$dev_make = $device["dev_make"];
$dev_model = $device["dev_model"];

} // END CUSTOMER DEVICE

echo "</table>";
} // END CLOSED S/O

if ($view == "detail") {
	// VIEW DETAIL

echo "							<div class='naglowek'><div class='pkt'></div>VIEW DETAIL</div>
							<div id='rightcont'>
";

$soid = $_GET["soid"];


// LETS GET SERVICE ORDER DATA
$get_so = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_id` = '$soid' LIMIT 1";  
$execute_get_so = mysql_query($get_so); 

while ($so = mysql_fetch_array($execute_get_so)){
	$so_id = $so["so_id"];
	$so_number = $so["so_number"];
	$so_cid = $so["so_cid"];
	$so_did = $so["so_did"];
	$so_pname = $so["so_pname"];
	$so_total = $so["so_total"];
	$so_discount = $so["so_discount"];
	$so_tax = $so["so_tax"];
	$so_note = $so["so_note"];
	$so_tnote = $so["so_tnote"];
	$so_status = $so["so_status"];
	$so_status_reason = $so["so_status_reason"];
	$so_opendate = $so["so_opendate"];
	$so_paydate = $so["so_paydate"];
	$so_paytype = $so["so_paytype"];
	$so_closedate = $so["so_closedate"];
	$so_refunddate = $so["so_refunddate"];
	$so_addby = $so["so_addby"];
	
	} // END GET SO DATA

$open_date = date("m.d.Y", $so_opendate);
$pay_date = date("m.d.Y", $so_paydate);


// LETS GET CUSTOMER DATA FROM DATABASE
$get_customer = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_id` = '$so_cid' LIMIT 1";  
$execute_get_customer = mysql_query($get_customer); 

while ($customer = mysql_fetch_array($execute_get_customer)){
$cust_id = $customer["cust_id"];
$cust_fname = $customer["cust_fname"];
$cust_lname = $customer["cust_lname"];
$cust_1phone = format_phone($customer["cust_1phone"]);
$cust_2phone = $customer["cust_2phone"];
$cust_email = $customer["cust_email"];
$cust_apt = $customer["cust_apt"];
$cust_street = $customer["cust_street"];
$cust_city = $customer["cust_city"];
$cust_state = $customer["cust_state"];
$cust_zip = $customer["cust_zip"];
$cust_comments = $customer["cust_comments"];
$cust_addby = $customer["cust_addby"];
$cust_addate = $customer["cust_addate"];
$cust_accnumber = $customer["cust_accnumber"];
}

// LETS GET CUSTOMER DEVICE INFO
$get_device = "SELECT * FROM `".$DBprefix."devices` WHERE `dev_id` = '$so_did' LIMIT 1";  
$execute_get_device = mysql_query($get_device); 

if ($device = mysql_fetch_array($execute_get_device)){
$dev_id = $device["dev_id"];
$dev_make = $device["dev_make"];
$dev_model = $device["dev_model"];

} // END CUSTOMER DEVICE


// LETS GET MODULES THAT ARE IN THE SYSTEM
$get_modules = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` LIKE 'Customers'";  
$execute_get_modules = mysql_query($get_modules); 

if ($module = mysql_fetch_array($execute_get_modules)){
	$cmod_id = $module["mod_id"];
	$cmod_name = $module["mod_name"];
	$cmod_location = $module["mod_location"];

} 




if ($so_status <= 0) { 
	
	$so_status = "<p class='InvStatus'>UNPAID</p>"; $sp1 = "SELECTED"; 
	$toolbar_links = "<A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A> <a href='print.php?soid=$so_id&to=I' target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </a> <a href=\"javascript:confirmDelete('modify.php?act=change&what=services&cat=so&soid=$so_id&status=1&modid=$modid','MARK AS PAID: $so_number')\" onmouseover=\"Tip('Mark As PAID')\"><img src='../g/ico/money_add.png'></a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id&modid=$modid','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Delete Inv.')\"><img src='../g/ico/so_delete.png'></a> <a href='print.php?soid=$so_id&to=P' target='_blank' onmouseover=\"Tip('Print Inv.')\"><img src='../g/ico/printer.png'></a> &nbsp; <a href='?show=mod&view=detail&modid=$cmod_id&cid=$cust_id' onmouseover=\"Tip('User Details')\"><img src='../g/ico/user_gray.png'></a>";

} else if ($so_status == 1) { 
	
	$so_status = "<p class='InvStatus1'>PAID</p>"; $sp2 = "SELECTED"; $db = "DISABLED";
	$toolbar_links = "<A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A> <a href='print.php?soid=$so_id&to=I' target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id&modid=$modid','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Delete Inv.')\"><img src='../g/ico/so_delete.png'></a> <a href='print.php?soid=$so_id&to=P' target='_blank' onmouseover=\"Tip('Print Inv.')\"><img src='../g/ico/printer.png' /></a> &nbsp; <a href='?show=mod&view=detail&modid=$cmod_id&cid=$cust_id' onmouseover=\"Tip('User Details')\"><img src='../g/ico/user_gray.png'></a>";
 

} else if ($so_status == 2) {

	$so_status = "<p class='InvStatus2'>REFUND</p>";  $sp3 = "SELECTED"; $db = "DISABLED";
	$toolbar_links = "<A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A> <a href='print.php?soid=$so_id&to=I' target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id&modid=$modid','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Delete Inv.')\"><img src='../g/ico/so_delete.png'></a> <a href='print.php?soid=$so_id&to=P' target='_blank' onmouseover=\"Tip('Print Inv.')\"><img src='../g/ico/printer.png' /></a> &nbsp; <a href='?show=mod&view=detail&modid=$cmod_id&cid=$cust_id' onmouseover=\"Tip('User Details')\"><img src='../g/ico/user_gray.png'></a>";


} else if ($so_status == 3) {

	$so_status = "<p class='InvStatus2'>CANCELED</p>"; $sp4 = "SELECTED"; $db = "DISABLED";
	$toolbar_links = "<A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A> <a href='print.php?soid=$so_id&to=I' target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id&modid=$modid','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Delete Inv.')\"><img src='../g/ico/so_delete.png'></a> <a href='print.php?soid=$so_id&to=P' target='_blank' onmouseover=\"Tip('Print Inv.')\"><img src='../g/ico/printer.png' /></a> &nbsp; <a href='?show=mod&view=detail&modid=$cmod_id&cid=$cust_id' onmouseover=\"Tip('User Details')\"><img src='../g/ico/user_gray.png'></a>";

} else { 

	$so_status = "<p class='InvStatus'>UNPAID</p>"; 

	$toolbar_links = "<A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A> <a href='print.php?soid=$so_id&to=I' target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </a> <a href=\"javascript:confirmDelete('modify.php?act=change&what=services&cat=so&soid=$so_id&status=1&modid=$modid','MARK AS PAID: $so_number')\" onmouseover=\"Tip('Mark As PAID')\"><img src='../g/ico/money_add.png'></a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id&modid=$modid','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Delete Inv.')\"><img src='../g/ico/so_delete.png'></a> <a href='print.php?soid=$so_id&to=P' target='_blank'><img src='../g/ico/printer.png' /></a>";
}

if ($so_paytype == 10) { $pt10 = "SELECTED"; } else if ($so_paytype <= 0) { $pt1 = "SELECTED"; } else if ($so_paytype == 1) { $pt2 = "SELECTED"; } else if ($so_paytype == 2) {$pt3 = "SELECTED";} else if ($so_paytype == 3) { $pt4 = "SELECTED"; }

if ($so_tax == 0 OR $so_tax == "") { $st0 = "SELECTED"; } else { $st1 = "SELECTED"; }

echo "
<form method='POST' action='modify.php?act=change'>
<input type='hidden' name='what' value='services'>
<input type='hidden' name='cat' value='so'>
<input type='hidden' name='soid' value='$so_id'>
<input type='hidden' name='sonumber' value='$so_number'>
<input type='hidden' name='modid' value='$modid'>
<div class='InvToolbar'>$toolbar_links</div>
<div class='InvMain'>
	<div class='InvCustInfo'>$cust_accnumber<br />$cust_fname $cust_lname<br/> $cust_street <br /> $cust_city, $cust_state $cust_zip <br />Phone: $cust_1phone <br />Email: $cust_email <br />";
if ($so_did != '0') { echo " Device: $dev_make $dev_model"; } else if (!empty($so_pname)) { echo "Project: $so_pname"; }

echo "

	</div>
    <div class='InvInfo'>
      <p>INVOICE#: <input class='input_form1' type='text' name='so_number' value='$so_number' readonly> <img class='calChange' src=\"../g/ico/blank.png\" /></p>
      <p>OPEN DATE: <input class='input_form1' type='text' name='fso_opend' value='$open_date' readonly> <img class='calChange' src=\"../g/ico/calendar_edit.png\" onclick=\"displayCalendar(document.forms[0].fso_opend,'mm.dd.yyyy',this)\" onmouseover=\"Tip('Edit Calendar')\" /></p>
      <p>DUE DATE: <input class='input_form1' type='text' name='fso_payd' value='$pay_date' readonly> <img class='calChange' src=\"../g/ico/calendar_edit.png\" onclick=\"displayCalendar(document.forms[0].fso_payd,'mm.dd.yyyy',this)\" onmouseover=\"Tip('Edit Calendar')\" /></p>
      <p>&nbsp;</p>      
      $so_status
      <p class='InvStatusR'>($so_status_reason)</p>
      
  </div>
    <div class='InvDetail'> 
<table class='InvTable' border='0' width='100%' cellspacing='0' cellpadding='0'>
	<tr>
		<td class='table5' width='6%'>QTY.</td>
		<td class='table5' width='45%'>SERVICE NAME</td>
		<td class='table5' width='10%'>PRICE</td>
		<td class='table5' width='5%'>DISCOUNT</td>
		<td class='table5' width='10%'>TAX</td>
		<td class='table5' width='10%'>TOTAL</td>
		<td class='table5' width='5%'>S/H</td>
		<td class='table5' width='9%'>&nbsp;</td>
	</tr>	";

// LETS GET SO DETAILS
$get_sod = "SELECT * FROM `".$DBprefix."service_details` WHERE `sod_soid` = '$soid' ORDER BY `sod_id` ASC";  
$execute_get_sod = mysql_query($get_sod); 

while ($sod = mysql_fetch_array($execute_get_sod)){

	$sod_id = $sod["sod_id"];
	$sod_soid = $sod["sod_soid"];
	$sod_servid = $sod["sod_servid"];
	$sod_servname = $sod["sod_servname"];
	$sod_servprice = $sod["sod_servprice"];
		if (empty($sod_servprice)) { $sod_servprice = "0"; }
	$sod_discount = $sod["sod_discount"];
		if (empty($sod_discount)) { $sod_discount = "0"; }
	$sod_shipping = $sod["sod_shipping"];
		if (empty($sod_shipping)) { $sod_shipping = "0"; }
	$sod_tax = $sod["sod_tax"];
		if (empty($sod_tax)) { $sod_tax = "0"; }
	$sod_taxtotal = $sod["sod_taxtotal"];
	$sod_qty = $sod["sod_qty"];
	$sod_total = $sod["sod_total"];
	$sod_addby = $sod["sod_addby"];
	
	if ($so_status <= 0) { $db1 = "<a href='$php_self?show=mod&modid=$mod_id&view=detail&soid=$soid&and=edit&sodid=$sod_id' onmouseover=\"Tip('Edit Item')\"><img src='../g/ico/soitem_edit.png' class='calChange' /></a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=soitem&sodid=$sod_id&soid=$so_id&modid=$modid','DELETING: $sod_servname - $$sod_servprice')\" onmouseover=\"Tip('Delete Item')\"><img src='../g/ico/soitem_delete.png' class='calChange' /> </a>"; }

	$item_subtotal = $sod_servprice * $sod_qty;
	if ($so_tax == 1) { $item_tax = $item_subtotal * 0.06; }
	$item_discount = ($sod_discount / 100) * $item_subtotal;
	
	$item_total = $item_subtotal - $item_discount;

	echo "
	 
	<tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
		<td>$sod_qty</td>
		<td>$sod_servname</td>
		<td>$ " .number_format($sod_servprice, 2, '.', ','). "</td>
		<td>$sod_discount%</td>
		<td>$ " .number_format($item_tax, 2, '.', ','). "</td>
		<td>$ " .number_format($item_total, 2, '.', ','). "</td>
		<td>$ " .number_format($sod_shipping, 2, '.', ','). "</td>
		<td>$db1</td>
	</tr>
	
	
	";
	
	$so_shTotal = $so_shTotal + $sod_shipping;
	$so_subTotal = $so_subTotal + $item_total;

}

if ($so_tax == 1) { $so_taxTotal = $so_subTotal * 0.06; }

$so_discTotal = ($so_discount / 100) * $so_subTotal;

$so_total = $so_subTotal + $so_taxTotal + $so_shTotal - $so_discTotal;

echo "
</table>
    </div>
    <div class='InvTotals'>

	    <div class='InvSumN'>
		<p>SUBTOTAL:</p>
		<p>DISCOUNT:</p>
		<p>TAX (6 %):</p>
		<p>SHIPPING:</p>
		<p>TOTAL:</p>
	    </div>
	    <div class='InvSumTN'>
		<p>$ " .number_format($so_subTotal, 2, '.', ','). "</p>
		<p>$ " .number_format($so_discTotal, 2, '.', ','). "</p>
		<p>$ " .number_format($so_taxTotal, 2, '.', ','). "</p>
		<p>$ " .number_format($so_shTotal, 2, '.', ','). "</p>
		<p class='InvSumTNR'>$ " .number_format($so_total, 2, '.', ','). "</p>
	    </div>

    </div>
</div>


<div class='InvOptions'>
<div class='form_title'>SO OPTIONS</div>
<div class='form_service'>

		<label for='so_did'>DEVICE:</label>&nbsp;
			<select id=\"so_did\" name=\"so_did\" class='input_select'><option value='0'>&nbsp; </option>";


// LETS GET CUSTOMER DEVICE INFO
$get_device = "SELECT * FROM `".$DBprefix."devices` WHERE `dev_custid` LIKE '$cust_id'";  
$execute_get_device = mysql_query($get_device); 

while ($device = mysql_fetch_array($execute_get_device)){
$dev_id = $device["dev_id"];
$dev_make = $device["dev_make"];
$dev_model = $device["dev_model"];

if ($dev_id == $so_did) { echo "<option value='$dev_id' SELECTED>$dev_make - $dev_model</option>\n"; } else { echo "<option value='$dev_id'>$dev_make - $dev_model</option>\n"; }

} // END CUSTOMER DEVICE


		echo "</select> <br />

		<label for='so_discount'>PROJECT NAME:</label>&nbsp;
			<input type='text' name='so_pname' value='$so_pname' class='input_form1' /><br />

		<label for='so_status'>MARK AS:</label>&nbsp;
			<select id=\"so_status\" name=\"so_status\" class='input_select' $sp1><option value='0'>UNPAID</option><option value='1' $sp2>PAID</option><option value='2' $sp3>REFUND</option><option value='3' $sp4>CANCELED</option></select><br />
		
		<label for='so_paytype'>PAYMENT TYPE:</label>&nbsp;
			<select id=\"so_paytype\" name=\"so_paytype\" class='input_select'><option value='10' $pt10> </option><option value='0' $pt1>CHECK</option><option value='1' $pt2>CASH</option><option value='2' $pt3>CREDIT</option><option value='3' $pt4>PAYPAL</option></select><br />


		<label for='so_status_reason'>PAYMENT NOTE:</label>&nbsp;
			<input id=\"so_status_reason\" name=\"so_status_reason\" value='$so_status_reason' class='input_form1' /><br />

		<label for='so_discount'>DISCOUNT:</label>&nbsp;
			<input type='text' name='so_discount' value='$so_discount' class='input_form1' /> %<br />

		<label for='so_tax'>TAX:</label>&nbsp;
			<select id=\"so_tax\" name=\"so_tax\" class='input_select'><option value='0' $st0>NO</option><option value='1' $st1>YES</option></select><br />

		<label for='so_note'>CUTOMER NOTE:</label>&nbsp;
			<textarea name='so_note' class='textarea_form'>$so_note</textarea><br /><br />

		<label for='so_note'>TECH NOTES:</label>&nbsp;
			<textarea name='so_tnote' class='textarea_form'>$so_tnote</textarea><br /><br />

		<input type=\"submit\" value='APPLY' class='button1'/>
		


</form>

</div>
</div>
";
$and = $_GET["and"];

if ($_GET["m"] == 29) { $and = "0"; }
if ($and == "edit") {
// LETS GET SO DETAILS
$get_sod = "SELECT * FROM `".$DBprefix."service_details` WHERE `sod_id` = '" .$_GET["sodid"]. "' ORDER BY `sod_id` ASC";  
$execute_get_sod = mysql_query($get_sod); 

if ($sod = mysql_fetch_array($execute_get_sod)){

	$sod_id = $sod["sod_id"];
	$sod_soid = $sod["sod_soid"];
	$sod_servid = $sod["sod_servid"];
	$sod_servname = $sod["sod_servname"];
	$sod_servprice = $sod["sod_servprice"];
	$sod_discount = $sod["sod_discount"];
	$sod_shipping = $sod["sod_shipping"];
	$sod_tax = $sod["sod_tax"];
	$sod_qty = $sod["sod_qty"];
	$sod_total = $sod["sod_total"];
	$sod_addby = $sod["sod_addby"];
}


echo "
<div class='InvAddItem'>
<div class='form_title'>EDIT SERVICE $db</div>
<form method='POST' action='modify.php?act=change' class='form_service'>
<input type='hidden' name='what' value='services'>
<input type='hidden' name='cat' value='soitem'>
<input type='hidden' name='sodid' value='$sod_id'>
<input type='hidden' name='soid' value='$sod_soid'>
<input type='hidden' name='modid' value='$modid'>

		<label for='serv_id'>SERVICE:</label>&nbsp; 
			<input id=\"add_service\" name=\"serv_name\" class='input_select' value='$sod_servname' $db /> <br />
		<label for='serv_qty'>PRICE:</label>&nbsp;
			<input type='text' name='serv_price' class='input_form1' value='$sod_servprice'/><br />
		<label for='serv_qty'>DISCOUNT:</label>&nbsp;
			<input type='text' name='serv_discount' class='input_form1' value='$sod_discount'/><br />
		<label for='serv_qty'>SHIPPING:</label>&nbsp;
			<input type='text' name='serv_shipping' class='input_form1' value='$sod_shipping' $db/><br />
		<label for='serv_qty'>QUANITY:</label>&nbsp;
			<input type='text' name='serv_qty' class='input_form1' value='$sod_qty' $db/><br /><br />
		<input type=\"submit\" value='CHANGE' class='button1' $db /> &nbsp;&nbsp;<a href='$php_self?show=mod&modid=$mod_id&view=detail&soid=$so_id'>Cancel</a>




</form>

</div>


";


} else {
echo "
<div class='InvAddItem'>
<div class='form_title'>ADD NEW SERVICE $db</div>
<form method='POST' action='modify.php?act=save' class='form_service'>
<input type='hidden' name='what' value='services'>
<input type='hidden' name='cat' value='soitem'>
<input type='hidden' name='soid' value='$so_id'>
<input type='hidden' name='sonumber' value='$so_number'>
<input type='hidden' name='modid' value='$modid'>

		<label for='serv_group'>GROUP:</label>&nbsp;  
			<select class='input_select' name='serv_group' onchange=\"getServices(this)\" $db><option value='0' SELECTED></option>";

$get_groups = mysql_query("SELECT * FROM `".$DBprefix."service_groups`");
while ($group = mysql_fetch_array($get_groups)){
$group_name = strtoupper($group["group_name"]);
$group_id = $group["group_id"];
echo "<option value='$group_id'>$group_name</option>\n";
}
 

echo "</select><br />
		<label for='serv_id'>SERVICE:</label>&nbsp; 
			<select id=\"add_service\" name=\"serv_id\" class='input_select' $db>  </select><br />
		<label for='serv_qty'>DISCOUNT:</label>&nbsp;
			<input type='text' name='serv_discount' class='input_form1' $db/><br />
		<label for='serv_qty'>SHIPPING:</label>&nbsp;
			<input type='text' name='serv_shipping' class='input_form1' $db/><br />
		<label for='serv_qty'>QUANITY:</label>&nbsp;
			<input type='text' name='serv_qty' class='input_form1' value='1' $db/><br /><br />
		<input type=\"submit\" value='ADD' class='button1' $db />		




</form>

</div>


";
} // END EDIT SERVICE



} // END VIEW DETAIL

if ($view == "add") {

echo "
							<div class='naglowek'><div class='pkt'></div>NEW SERVICE</div>
							<div id='rightcont'>
";

echo "<form method='POST' action='modify.php?act=save' name=\"form_newservice\" onsubmit=\"return validate_form();\">";


echo "Lets Start With a Customer:<br />

<input class='input_form'  type=\"text\" size=\"23\" id=\"cust\" name=\"cust\" value=\"\" autocomplete=\"off\" onkeyup=\"ajax_showOptions(this,'getCountriesByLetters',event)\"><br /><br />
<input type='radio' name='so_type' value='1' /> Service Order<br />
<input type='radio' name='so_type' value='2' /> Quote<br />
<input type=\"hidden\" id=\"cust_hidden\" name=\"cust_ID\">
<input type=\"hidden\" name=\"what\" value=\"services\">
<input type=\"hidden\" name=\"cat\" value=\"so\">
<input type=\"hidden\" name=\"modid\" value=\"$modid\">

 

<br /><br /><input type='submit' value='Next' class=\"button1\">


</form>


";




} // END ADD



if ($view == "search") {

echo "
							<div class='naglowek'><div class='pkt'></div>SEARCH</div>
							<div id='rightcont'>
";

} // END SEARCH

if ($view == "services") {

echo "
							<div class='naglowek'><div class='pkt'></div>SERVICES</div>
							<div id='rightcont'>

<table style='width: 100%' class='editform1'>
	<tr>
		<td width='20%' class='table5' style='text-align: center'>GROUP</td>
		<td width='30%' class='table5' style='text-align: center'>NAME</td>
		<td width='10%' class='table5' style='text-align: center'>BASE PRICE</td>
		<td width='15%' class='table5' style='text-align: center'>DISCOUNT</td>
		<td width='10%' class='table5' style='text-align: center'>ADD BY</td>
		<td width='15%' class='table5'>OPTIONS</td>
	</tr>
";


// LETS GET SERVICES FROM DATABASE
$get_services = "SELECT * FROM `".$DBprefix."services` ORDER BY `serv_group`,`serv_name` ASC";  
$execute_get_services = mysql_query($get_services); 

while ($service = mysql_fetch_array($execute_get_services)){

$serv_id = $service["serv_id"];
$serv_group = $service["serv_group"];
$serv_name = $service["serv_name"];
$serv_discount = $service["serv_discount"];
$serv_bprice = $service["serv_bprice"];
$serv_desc = stripcslashes(stripslashes($service["serv_desc"]));
$serv_addby = $service["serv_addby"];

$get_username = mysql_query("SELECT `user_name` FROM `".$DBprefix."signup` WHERE `user_id` = '$serv_addby'");
while ($user = mysql_fetch_array($get_username)){
$serv_addby = strtoupper($user["user_name"]);
}

$get_groupname = mysql_query("SELECT `group_name` FROM `".$DBprefix."service_groups` WHERE `group_id` = '$serv_group'");
while ($group = mysql_fetch_array($get_groupname)){
$group_name = strtoupper($group["group_name"]);
}

if ($serv_bprice == 0) { $serv_bprice = "0"; }
if ($serv_discount == 0) { $serv_discount = "0"; }
echo "
	<tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
		<td class='table6'>$group_name</td>
		<td class='table6'>$serv_name</td>
		<td class='table6'>$" .number_format($serv_bprice, 2, '.', ','). "</td>
		<td class='table6'>" .number_format($serv_discount, 2, '.', ','). "%</td>
		<td class='table6'>$serv_addby</td>
		<td class='table6'><!-- <img src='../g/ico/device_edit.png'> --> &nbsp;<a href='modify.php?act=delete&what=services&cat=service&modid=$modid&sid=$serv_id'><img src='../g/ico/device_delete.png'></a> <img src='../g/ico/info.png' onmouseover=\"Tip('$serv_desc')\" /></td>
	</tr>
";

}



echo "

</table>
<br /><br /><div class='form_title'>ADD NEW SERVICE</div>
<form method='POST' action='modify.php?act=save' class='form_service'>
<input type='hidden' name='what' value='services'>
<input type='hidden' name='cat' value='service'>
<input type='hidden' name='modid' value='$modid'>
		<label for='serv_group'>GROUP:</label>&nbsp;  
			<select name=\"serv_group\" class=\"input_select\">";

$get_groups = mysql_query("SELECT * FROM `".$DBprefix."service_groups`");
while ($group = mysql_fetch_array($get_groups)){
$group_name = strtoupper($group["group_name"]);
$group_id = $group["group_id"];
echo "<option value='$group_id'>$group_name</option>\n";
}
 

echo "</select><br />
		<label for='serv_name'>NAME:</label>&nbsp; 
			<input name=\"serv_name\" type=\"text\" class=\"input_form2\" size=\"50\" /><br />
		<label for='serv_discount'>DISCOUNT:</label>&nbsp; 
			<input name=\"serv_discount\" type=\"text\" class=\"input_form2\" size=\"50\" ><br />
		<label for='serv_bprice'>BASE PRICE:</label>&nbsp;  
			<input name=\"serv_bprice\" type=\"text\" class=\"input_form2\" /> <br />
		<label for='serv_bprice'>DESCRIPTION:</label>&nbsp;  
			<textarea name=\"serv_desc\" type=\"text\" class=\"textarea_form\"> </textarea> <br /><br />
		<input type=\"submit\" value='ADD SERVICE' class='button1'/>
		




</form>

";

}// END SERVICES

if ($view == "groups") {

echo "
							<div class='naglowek'><div class='pkt'></div>GROUPS</div>
							<div id='rightcont'>

<table style='width: 100%' class='editform1'>
	<tr>
		<td width='40%' class='table5' style='text-align: center'>GROUP</td>
		<td width='40%' class='table5' style='text-align: center'>ADD BY</td>
		<td width='20%' class='table5'>OPTIONS</td>
	</tr>
";


// LETS GET SERVICES FROM DATABASE
$get_service_groups = "SELECT * FROM `".$DBprefix."service_groups` ORDER BY `group_name` ASC";  
$execute_get_service_groups = mysql_query($get_service_groups); 

while ($group = mysql_fetch_array($execute_get_service_groups)){

$group_id = $group["group_id"];
$group_name = $group["group_name"];
$group_desc = $group["group_desc"];
$group_addby = $group["group_addby"];

$get_username = mysql_query("SELECT `user_name` FROM `".$DBprefix."signup` WHERE `user_id` = '$group_addby'");
while ($user = mysql_fetch_array($get_username)){
$group_addby = strtoupper($user["user_name"]);
}


echo "
	<tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
		<td class='table6'>$group_name</td>
		<td class='table6'>$group_addby</td>
		<td class='table6'><!-- <img src='../g/ico/device_edit.png'> --> &nbsp;<a href='modify.php?act=delete&what=services&cat=group&modid=$modid&gid=$group_id'><img src='../g/ico/device_delete.png'></a> <img src='../g/ico/info.png' onmouseover=\"Tip('$group_desc')\" /></td>
	</tr>
";

}



echo "

</table>
<form method='POST' action='modify.php?act=save'>
<input type='hidden' name='what' value='services'>
<input type='hidden' name='cat' value='group'>
<input type='hidden' name='modid' value='$modid'>
<br />
<table style='width: 100%' class='editform1'>
	<tr>
		<td  style='text-align: center'>NAME: <input name=\"group_name\" type=\"text\" class=\"input_form1\" size=\"50\"></td>
		<td  style='text-align: center'>DESCRIPTION: <input name=\"group_desc\" type=\"text\" class=\"input_form1\"></td>
		<td width='10%' style='text-align: center'><input type=\"image\" src=\"../g/ico/device_add.png\" /></td>


	</tr>
</table>
</form>

";

}// END SERVICES




?>

</div>
